package com.drama.api.sys.user.controller;

import com.drama.api.base.*;
import com.drama.api.sys.user.domain.vo.*;
import com.drama.api.sys.user.service.ISysUserService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysUserController)
@RequestMapping(value = "/api/sys/user")
@RestController
public class SysUserController {


    @Autowired
    private ISysUserService service;

    @ApiOperation(value = "查询平台用户列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<SysUserVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysUserSearchVO searchVO) {
        List<SysUserVO> list = service.getList(searchVO);
        list=service.getRole(list);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建平台用户")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysUserAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除平台用户")
    @PostMapping(value = "/delete")
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }

    @ApiOperation(value = "禁言/启用-平台用户")
    @PostMapping(value = "/disableUser")
    public R disableUser(@ApiParam(value = "参数")  @RequestBody SysUserStateVO vo) {
        service.disableUser(vo);
        return new R();
    }

    @ApiOperation(value = "修改平台用户")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody SysUserUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }


    @ApiOperation(value = "重置平台用户密码")
    @PostMapping(value = "/resetPassword")
    public R resetPassword(@ApiParam(value = "用户ID") @Valid @RequestBody SysUserPasswordVO updateVO) {
        service.resetPassword(updateVO);
        return new R();
    }


}
